package com.charity.contoller;

import com.alibaba.fastjson.JSONArray;
import com.charity.mapper.DonationMapper;
import com.charity.model.CommonResponse;
import com.charity.pojo.Advertisement;
import com.charity.pojo.Donation;
import com.charity.pojo.Entry;
import com.charity.pojo.User;
import com.charity.service.AdvertisementService;
import com.charity.service.DonationService;
import com.charity.service.EntryService;
import com.charity.service.UserService;
import com.charity.utils.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class ShareController {

    public static final String PubKey = com.charity.utils.IOUtil.readResourceAsString("conf/PubKey");
    public static final String PriKey = com.charity.utils.IOUtil.readResourceAsString("conf/PriKey");

    @Autowired
    private AdvertisementService advertisementService;

    @Autowired
    private DonationService donationService;

    @Autowired
    private DonationMapper donationMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private EntryService entryService;

    private CommonResponse commonResponse = new CommonResponse();

    private RSAUtil rsaUtil = new RSAUtil();

    @RequestMapping("/advertisements")
    public CommonResponse advertisements() {
        List<Advertisement> advertisements1 = advertisementService.advertisements();
        String advertisements = JSONArray.toJSONString(advertisements1);
        return commonResponse.ok(advertisements1);
    }

    @GetMapping("/advertisement")
    public CommonResponse advertisement(Integer no) {
        Advertisement advertisement = advertisementService.advertisement(no);
        if (advertisement != null) {
            return commonResponse.ok(advertisement);
        }
        return commonResponse.cuw("300", "没有该公告");
    }

    @RequestMapping("/bangDan")
    public CommonResponse bangDan() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        System.out.println(month);
        List<Donation> ban = donationService.bangDan(month);
        List<Donation> ban1 = new ArrayList<>();
        for (Donation donation: ban) {
            donation.setCreateDate(sdf.format(new Date()));
            ban1.add(donation);
        }
        String banDan = JSONArray.toJSON(ban).toString();
        return commonResponse.ok(ban1);
    }

    @RequestMapping("/entryByFbu")
    public CommonResponse entryByFbu(){
        List<Entry> entries = entryService.entryByFbu();
        String entryByFbu = JSONArray.toJSON(entries).toString();
        return commonResponse.ok(entries);
    }

    @RequestMapping("/entryById")
    public CommonResponse entryById(Long id){
        Entry entry = entryService.entryById(id);
        if (entry != null) {
            return commonResponse.ok(entry);
        }
        return commonResponse.cuw("300","没有此受助单");
    }

    @RequestMapping("/userBySZ")
    public CommonResponse userBySZ() throws Exception {
        List<User> users = userService.userBySZ();
        List<User> users1 = new ArrayList<>();
        for (User user:users) {
            user.setRealName(rsaUtil.decrypt(user.getRealName(), PriKey));
            user.setIdCard(rsaUtil.decrypt(user.getIdCard(), PriKey));
            users1.add(user);
        }
        if (users != null) {
            return commonResponse.ok(users1);
        }
        return commonResponse.cuw("300","没有此受助单");
    }

    @RequestMapping("/selQ")
    public CommonResponse selQ() throws Exception {
        List<Donation> donations = donationMapper.selQ();
        return commonResponse.ok(donations);
    }

}
